﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using Soveryn.Commerce.Shared;

/// <summary>
/// This is the base class for all doodads. It handles custom properties and doodad initialization. It also displays the doodad toolbar if the user is in edit mode.
/// </summary>

namespace Soveryn.Commerce.BaseClasses
{


    public class DoodadBaseClass : ControlBaseClass
    {

        public DoodadInstanceProperties Properties = new DoodadInstanceProperties();
        public Dictionary<String, DoodadInstanceProperty> UserProperties;

        public Control Canvas;

        public string GetPropertyValue(String name)
        {

            if (UserProperties != null && UserProperties.ContainsKey(name) && !String.IsNullOrEmpty(UserProperties[name].Value))
                return UserProperties[name].Value;
            else
                return "";

        }

        public string GetPropertyValue(String name, String defaultValue)
        {

            if (UserProperties != null && UserProperties.ContainsKey(name) && !String.IsNullOrEmpty(UserProperties[name].Value))
                return UserProperties[name].Value;
            else
                return defaultValue;

        }

        protected void Page_Load(object sender, EventArgs e)
        {

            //setup properties
            if (!String.IsNullOrEmpty(Properties.DoodadInstancePropertyXML))
            {
                UserProperties = DoodadUtils.ProcessProperties(Properties.DoodadInstancePropertyXML);
                foreach (KeyValuePair<string, DoodadInstanceProperty> prop in UserProperties)
                    prop.Value.Value = DoodadUtils.ReplaceUserVars(prop.Value.Value, Properties, ParentPage.Website);
            }

            //replace user vars for html
            Properties.DoodadInstanceHTML = DoodadUtils.ReplaceUserVars(Properties.DoodadInstanceHTML, Properties, ParentPage.Website);

            //get start/end placeholders
            PlaceHolder startDoodad = (PlaceHolder)FindControl("StartDoodad");
            PlaceHolder endDoodad = (PlaceHolder)FindControl("EndDoodad");

            //add start code
            Literal startLiteral = new Literal();

            if (ParentPage.EditMode)
                Properties.DoodadInstanceCSSClass += " doodadHover";

            startLiteral.Text = "<div class='" + ("doodad " + (Properties.DoodadInstanceCSSClass).Trim()).Trim() + "' id='" + Properties.ElementID + "' data-order='" + Properties.DoodadInstanceLoadOrder.Value.ToString() + "' style='" + Properties.DoodadInstanceInlineCSS + "'>";

            //add end code
            Literal endLiteral = new Literal();
            endLiteral.Text = "</div>";

            //add literals
            startDoodad.Controls.Add(startLiteral);
            endDoodad.Controls.Add(endLiteral);

            //add toolbar
            if (ParentPage.EditMode)
            {

                DoodadToolBarBaseClass DoodadToolBar = (DoodadToolBarBaseClass)LoadControl("~/Controls/DoodadToolBar.ascx");
                DoodadToolBar.doodad = Properties;
                startDoodad.Controls.Add(DoodadToolBar);
            }

        }

    }
}